<?php
namespace BoxAdmin\Controller;

use Think\Controller;

class AuthController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->id = I('id', 0, 'intval');

    }

    /**
     * 管理员列表
     * @return [type] [description]
     */
    public function index()
    {
        $model = BoxModel('system_store_user');
        $where = $this->isSuper ? "1 " : "store_id={$this->store_id}";
        if ($admin_id = session('admin_id') && $this->admin_user_info['pid'] > 0) {
            $where .= " pid = '$admin_id'";
        }
        $order = 'id desc';
        $data = $model->get($where, 10, $order);
        $this->data = $data['list'];
        $this->show_page($data['pager']);
        $this->display();
    }

    /**
     *添加管理员
     */
    public function admin_add()
    {
        $this->display();
    }

    public function admin_add_do()
    {
        if (IS_POST) {
            $user_name = I('user_name');
            $password = I('password', '', 'encrypt');
            $add = array(
                'user_name' => $user_name,
                'password' => $password,
                'store_id' => $this->store_id,
                'pid' => intval(session('admin_id')),
                'pasttime' => time(),
            );
            BoxModel('system_store_user')->add($add);
            $this->jump("添加成功", U('index'));
        }
    }

    /**
     * 检测用户名
     */
    public function check_user()
    {
        if (IS_AJAX) {
            $user_name = I('user_name');
            $find = BoxModel('system_store_user')->where(array('user_name' => $user_name))->find();
            if ($find) {
                $isAvailable = 'false';
            } else {
                $isAvailable = 'true';
            }
            echo json_encode(array(
                'valid' => $isAvailable,
            ));
        }
    }

    /**
     * 禁止管理员
     */
    public function admin_lock()
    {
        if ($id = I('id')) {
            $status = I('status');
            BoxModel('system_store_user')->where(array('id' => $id))->setField('status', $status);
            $this->jump("操作成功");
        }
    }

    /**
     * 删除管理员
     * @return [type] [description]
     */
    public function admin_del()
    {
        if ($id = I('id')) {
            BoxModel('system_store_user')->delete($id);
            $this->jump("删除成功");
        }
    }

    /**
     * 分配权限
     */
    public function access()
    {
        $this->memuNav = "index";
        $admin_id = I('admin_id');
        $userData = BoxModel("system_store_user")->where("id='$admin_id'")->find();

        if (!$userData) $this->jump("参数错误");

        /****商家可选权限***/
        //$R=BoxModel("system_store_user")->where("id='$id'")->find();
        $s_admin_id = $this->admin_user_info['id'];//商家最高管理员
        $store_auth = BoxModel('addon_auth_access')->where(array('admin_id' => $s_admin_id))->find();

        $ids = $store_auth['access'];
        $model = BoxModel('addon_auth_group');
        /*****超级管理员****/
        $where = "1";
        if ($this->isSuper) {
            $ids = "";
        }
        if ($ids) {
            $where .= " and rid in ($ids)";                                        //可分配的权限组条件
        }
        $order = 'order_by asc';
        $rule = BoxModel('addon_auth');

        $data = $model->where($where)->order($order)->select();                //可分配的权限组
        //dump($model);
        $access = BoxModel('addon_auth_access')->where(array('admin_id' => $admin_id))->find();
        //dump($admin_id);
        //dump($access);
        $this->access = $access = explode(',', $access['access']);
        foreach ($data as $k => $v) {
            foreach ($access as $value) {
                if ($value == $v['rid']) {
                    $data[$k]['checked'] = "checked='checked'";
                }
            }
            $group_id = $v['id'];
            $where = "group_id='$group_id'";
            if ($ids) {
                $where .= " and id in ($ids)";
            }
            $rules = $rule->where($where)->select();
            $data[$k]['rule'] = $rules;
        }
        foreach ($data as $key => $value) {
            foreach ($data[$key]['rule'] as $k => $v) {
                foreach ($access as $vv) {
                    if ($vv == $v['id']) {
                        $data[$key]['rule'][$k]['checked'] = "checked='checked'";
                    }
                }
            }
        }
        $this->data = $data;
        $this->display();
    }


    public function access_add_do()
    {
        if (IS_POST) {
            $ids = I('ids');
            $ids = rtrim($ids, ',');
            $admin_id = I('admin_id');
            $model = BoxModel('addon_auth_access');
            $store_user = BoxModel("system_store_user")->where(array('id' => session('admin_id')))->find();
            $find = $model->where(array('admin_id' => $admin_id))->find();
            /**********权限分配验证*******/
            if ($this->store_type == 2) {
                $r = $model->where(array('admin_id' => session('admin_id')))->find();
                $access_ids = explode(",", $r['access']);
                $ids_array = explode(",", $ids);
                foreach ($ids_array as $id) {
                    if (!in_array($id, $access_ids)) {
                        $this->jump("权限超出你的所拥有");
                        exit();
                    }
                }
            }

            /***********************给用户分配图标*********/
            $auth_group_user = BoxModel("addon_auth_group_user");
            $auth_group_user->where("admin_id='$admin_id'")->delete();
            $group_ids = explode(",", I('group_ids_val'));
            //	dump($group_ids);exit();
            foreach ($group_ids as $group_id) {
                $row = array(
                    'group_id' => $group_id,
                    'admin_id' => $admin_id,
                );
                $r = $auth_group_user->add($row);
                if (!$r) {
                    dump($auth_group_user);
                }
            }
            /**********给用户分配权限********************/
            $data = array('access' => $ids);
            if ($find) {
                $model->where(array('admin_id' => $admin_id))->save($data);
            } else {
                $data['admin_id'] = $admin_id;
                $model->add($data);
            }
            $this->jump('操作成功');
        }
    }

    /*******显示图标**/
    public function add_group_ico()
    {
        if (session('isSuper')) {
            $this->data = BoxModel('addon_auth_group')->get("is_show=1");
        } else {
            $this->data = BoxModel('addon_auth_group_user')->join("addon_auth_group as G on G.id=addon_auth_group_user.group_id")->order("addon_auth_group_user.order_by")->where(array('admin_id' => session('admin_id'), 'G.is_show' => 1))->field("addon_auth_group_user.id,G.title,status")->select();
        }
        $this->display();
    }


    function add_group_ico_act()
    {
        $id = I('id');
        $data = BoxModel('addon_auth_group_user')->find($id);
        $status = $data['status'] ? 0 : 1;
        BoxModel('addon_auth_group_user')->save(array('id' => $id, 'status' => $status));
    }

    /**
     * [delete 删除日志]
     * @return [type] [description]
     */
    public function delete()
    {
        $ids = I('get.ids');
        switch (I('get.operation', '', 'intval')) {
            case 1:
                $this->model->where(array('id' => $this->id))->delete();
                break;
            case 2:
                $this->model->where('id in(' . $ids . ')')->delete();
                break;
        }
        $this->redirect('Auth/log');
    }

}   